import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/message.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/data/message_data_entity.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/widget/asr_app_bar.dart';

class FaxianSystemPage extends StatefulWidget {
  const FaxianSystemPage({Key? key}) : super(key: key);

  @override
  _FaxianSystemPageState createState() => _FaxianSystemPageState();
}

class _FaxianSystemPageState extends State<FaxianSystemPage> {

  int pageNum = 1;
  int pageSize = 1;

  List<MessageItem> messages = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getList();
  }

  getList() async {
    ResponseData res = await MessageAPI.getMessageList(pageNum, pageSize,1);
    if (res.code == 200) {
      if(pageNum==1){
        messages.clear();
      }
      List arr = res.data??[];
      for (var element in arr) {
        MessageItem item = MessageItem().fromJson(element);
        messages.add(item);
      }
    }
    if(mounted){setState(() {
    });}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AsrAppBar(
        titleText: '系统通知',
      ),
      body: Container(
        padding: EdgeInsets.symmetric(vertical: 40.w),
        child: EasyRefresh(
          header: myRefreshHeader(),
          footer: myRefreshFooter(),
          onRefresh: ()async{
            pageNum=1;
            getList();
          },
          onLoad: ()async{
            pageNum++;
            getList();
          },
          child: ListView.builder(
              itemCount: messages.length,
              itemBuilder: (context,index){
                MessageItem item = messages[index];
                return Container(
                  margin: EdgeInsets.only(bottom: 50.w),
                  child: Column(
                    children: [
                      Text(item.createTime!,style: TextStyle(
                        color: Color(0xff848484),
                        fontSize: 28.sp,
                      )),
                      SizedBox(height: 20.w,),
                      Row(
                        children: [
                          Expanded(child: Container(
                            padding: EdgeInsets.all(40.w),
                            margin: EdgeInsets.symmetric(horizontal: 40.w),
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(20.w)
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Visibility(child: Text('${item.title??''}',
                                  style: TextStyle(
                                      color: Colors.black,
                                      fontSize: 32.sp,
                                      fontWeight: FontWeight.w600
                                  ),),visible: item.title!=null,),
                                Visibility(child: SizedBox(height: 40.w,),visible: item.title!=null,),
                                Text('${item.content??''}',style: TextStyle(
                                  color: Color(0xff848484),
                                  fontSize: 28.sp,
                                ))
                              ],
                            ),
                          ))
                        ],
                      )
                    ],
                  ),
                );
              }),
        ),
      ),
    );
  }
}
